package admin

import (
	_ "embed"
	. "go-caipu/pkg/services/sqlstore/migrator"
)

//go:embed sql/sys_user.sql
var userSQLByte []byte

//go:embed sql/sys_user_role.sql
var userRoleSQLByte []byte

func AddUserMigrations(mg *Migrator) {
	userV1 := Table{
		Name: "sys_user",
		Columns: []*Column{
			{Name: "user_id", Type: DB_BigInt, IsPrimaryKey: true},
			{Name: "username", Type: DB_Varchar, Length: 64, Nullable: false},
			{Name: "password", Type: DB_NVarchar, Length: 128, Nullable: false},
			{Name: "nick_name", Type: DB_NVarchar, Length: 128, Nullable: false},
			{Name: "phone", Type: DB_NVarchar, Length: 128, Nullable: true},
			{Name: "role_id", Type: DB_BigInt, Length: 20, Nullable: true},
			{Name: "salt", Type: DB_NVarchar, Length: 255, Nullable: true},
			{Name: "avatar", Type: DB_NVarchar, Length: 255, Nullable: true},
			{Name: "sex", Type: DB_TinyInt, Length: 2, Nullable: true},
			{Name: "email", Type: DB_NVarchar, Length: 128, Nullable: false},
			{Name: "dept_id", Type: DB_NVarchar, Length: 20, Nullable: false},
			{Name: "post_id", Type: DB_NVarchar, Length: 20, Nullable: false},
			{Name: "status", Type: DB_BigInt, Length: 4, Nullable: false},
			{Name: "remark", Type: DB_NVarchar, Length: 255, Nullable: true},

			{Name: "create_by", Type: DB_BigInt, Nullable: false},
			{Name: "update_by", Type: DB_BigInt, Nullable: true},
			{Name: "created_at", Type: DB_DateTime, Nullable: false},
			{Name: "updated_at", Type: DB_DateTime, Nullable: true},
		},
		Indices: []*Index{
			{Cols: []string{"username"}, Type: UniqueIndex},
			{Cols: []string{"email"}, Type: UniqueIndex},
			{Cols: []string{"phone"}, Type: UniqueIndex},
		},
	}
	userRoleV1 := Table{
		Name: "sys_user_role",
		Columns: []*Column{
			{Name: "user_id", Type: DB_BigInt, Length: 20, Nullable: false},
			{Name: "role_id", Type: DB_BigInt, Length: 64, Nullable: false},
		},
		Indices: []*Index{
			{Cols: []string{"user_id", "role_id"}, Type: UniqueIndex},
		},
	}
	//第三方用户表
	userThird := Table{
		Name: "sys_user_third",
		Columns: []*Column{
			{Name: "user_id", Type: DB_BigInt, Length: 20, Nullable: false, IsPrimaryKey: true, IsAutoIncrement: true},
			{Name: "third_id", Type: DB_NVarchar, Length: 50, Nullable: false},
			{Name: "third_type", Type: DB_NVarchar, Length: 30, Nullable: false},
		},
		Indices: []*Index{
			{Cols: []string{"third_type", "third_id", "user_id"}, Type: UniqueIndex},
		},
	}
	//create table
	mg.AddMigration("create sysuser table", NewAddTableMigration(userV1))
	mg.AddMigration("create user_role table", NewAddTableMigration(userRoleV1))
	mg.AddMigration("create sys_user_third table", NewAddTableMigration(userThird))
	//add: admin-user
	mg.AddMigration("add admin sysuser", NewRawSQLMigration(string(userSQLByte)))
	mg.AddMigration("add admin sys_user_role", NewRawSQLMigration(string(userRoleSQLByte)))
	mg.AddMigration("add admin index_user_role", NewAddIndexMigration(userRoleV1, userRoleV1.Indices[0]))
	//第三方用户表添加索引
	mg.AddMigration("add user index_user_third", NewAddIndexMigration(userThird, userThird.Indices[0]))

}
